﻿
$(function () {
    _vmBase = new viewModel();

    _vmBase.init();


})


function viewModel() {
    var me = this;
    VueViewModelBase.call(me);
    viewModelExtend.call(me);

    me.orgTreeUrl = _getTreeDataUrl;
    me.tableListUrl = _getModelsUrl;
    me.addUrl = _addModelUrl;
    me.delUrl = _deleteModelUrl;
    me.tabObj = $('#tb_orgs');


    

    //重写方法
    me.init = function () {

        me.bindTree(me.orgTreeUrl,"orgTree");

        me.initTableList();

        //添加监听对象
        me.addMonitor();

        me.InitControl();
    }

    me.initTableList = function () {
        var options = {
            search: false,
            columns: [
                {
                    checkbox: true
                },
                {
                    field: 'fullName',
                    title: '名称'
                },
                {
                    field: 'indexId',
                    title: '节点序号'
                },
                {
                    field: 'sortIndex',
                    title: '序号'
                },
                {
                    field: 'enCode',
                    title: '编号'
                },
                {
                    field: 'categoryId',
                    title: '类型',
                    formatter: function (value, row, index) {
                        return exFunc(row.categoryId);
                    }

                },
                {
                    field: 'parentName',
                    title: '父节点'
                },
                {
                    field: 'createdOn',
                    title: '创建时间',
                    formatter: function (value, row, index) {
                        return DateFormat(row.createdOn);
                    }
                    
                },
                {
                    field: 'description',
                    title: '备注'
                },

                {
                    field: 'enabledMark',
                    title: '状态',
                    formatter: function (value, row, index) {
                        var enabledLabel = '<span class="label label-success">' + _enabledStr + '</span>';
                        var disabledLabel = '<span class="label label-default">' + _disabledStr + '</span>';
                        if (row.enabledMark) {
                            return enabledLabel;
                        } else {
                            return disabledLabel;
                        }
                    }
                }
            ]
        }
        me.initList(options);

    }

    //me.add = function () {
    //    var selector = "select2-CategoryId-container";
    //    $('#' + selector).html("==请选择==");
    //    $('#' + selector).attr('title', "==请选择==");
    //}

    //me.edit = function () {

    //    var selector = "select2-CategoryId-container";
        

    //    if (vueModel.selectedData) {
    //        var selectRows = me.selections;

    //        var lastSelectObj = selectRows[selectRows.length - 1];

    //        me.addMonitor(lastSelectObj);

    //        $('#' + selector).html(exFunc(lastSelectObj.categoryId));
    //        $('#' + selector).attr('title', lastSelectObj.categoryId);

    //    } else {
    //        $dh.msg(_SelectADataMsg);//请选择一条数据
    //    }
    //}

    me.save = function (callback) {
        if (!$('#form1').formValid()) {
            return false;
            callback(false);
        }
        var model = vueModel.$data.entityObj;
        
        var checkUrl = _checkExistSortUrl + "?parentId=" + model.ParentId + "&sortIndex=" + model.SortIndex;
        if (model.Id != null && model.Id != undefined && model.Id != "") {
            checkUrl = checkUrl + "&Id=" + model.Id;
        }
        $dh.get(checkUrl, null, function (result) {
            if (result.Status == ResultStatus.OK && result.Data.res == false) {
                $dh.confirm(result.Msg, function () {
                    me.submit(model, callback);
                });

            } else {
                me.submit(model, callback);
            }


        });
    }

    me.delete = function (callback) {
        if (!vueModel.selectedData) {

            $dh.msg(_SelectADataMsg); //请选择一条数据
        } else {
            var selectRows = me.selections;
            var ids = [];
            for (var idIndex = 0; idIndex < selectRows.length; idIndex++) {
                ids.push(selectRows[idIndex].id);
            }

            $dh.confirm(_deleteRemand, function () {
                //确认删除
                $dh.post(me.delUrl, { ids: ids }, function (result) {
                    if (result.Status == ResultStatus.OK) {

                        me.RefreshControl();

                        callback(result, true);

                    } else {
                        callback(result, false);

                    }
                });

            });

        }

        //callback(true);
    }

    me.addMonitor = function (monitorObj) {
        var targetObj = vueModel.entityObj;

        if (monitorObj != null && monitorObj != undefined) {
            me.addMonitorObj(targetObj, monitorObj);
        } else {
            var addObj = {
                Id: '',
                ParentName: '',
                ParentId: '',
                CategoryId: '',
                FullName: '',
                EnCode: '',
                SortIndex: '',
                ManagerId: '',
                MobilePhone: '',
                WeChat: '',
                TelePhone: '',
                Email: '',
                Fax: '',
                Address: '',
                EnabledMark: true,
                Description: ''
            };
            me.addMonitorObj(targetObj, addObj);
        }
    }

    

}

function viewModelExtend() {
    var me = this;
    

    me.InitControl = function () {

        //$('#CategoryId').bindSelect2();

        me.bindUserOrg();
    }

    me.bindUserOrg = function () {
        $("#ParentName").on("click", function () {
            _parentOrgTree.reload();
        });
        _parentOrgTree = new ParentTree(me.orgTreeUrl, "ParentName", "ParentId", "parentOrgTree", vueModel.entityObj);

    }

    /*重写刷新控件*/
    me.RefreshControl = function () {

        me.reloadOrgTree();

    }

    me.reloadOrgTree = function () {
        var treeObj = $.fn.zTree.getZTreeObj("orgTree");
        treeObj.destroy();
        me.bindTree(me.orgTreeUrl, "orgTree");
    }

    me.submit = function (model, callback) {
        $dh.post(me.addUrl, model, function (result) {
            if (result.Status == ResultStatus.OK) {

                me.RefreshControl();

                callback(result, true);
            } else {
                callback(result, false);
            }
        })
    }

}


function exFunc(typeObj) {
    var typeValue = typeObj;
    if (typeValue == "Group") {
        return "集团";
    } else if (typeValue == "Company") {
        return "公司";
    } else if (typeValue == "Department") {
        return "部门";
    } else if (typeValue == "WorkGroup") {
        return "小组";
    }
    return "";
}
